/**
 * 
 */
package ken.app.xmlhandler;

import java.util.ArrayList;
import java.util.List;

import ken.app.bean.CHTContentUploadBean;
import ken.app.bean.PersonalBean;
import ken.app.bean.QueryPersonCouponBean;
import ken.app.bean.TreeAllBean;
import ken.app.bean.TreeSingleBean;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * @author ken.chung
 * 
 */
public class QueryPersonCouponXMLHandler extends DefaultHandler {
	/* 變數宣告 */
	private boolean in_item = false;
	private boolean in_gift_id = false;
	private boolean in_gift_code = false;
	private boolean in_gift_name = false;
	private boolean in_date = false;
	private boolean in_date_display = false;

	private List<QueryPersonCouponBean> list;
	private QueryPersonCouponBean queryPersonCouponBean;
//	private String title = "";
	private StringBuffer buf = new StringBuffer();

	/* 將轉換成List<News>的XML資料回傳 */
	public List<QueryPersonCouponBean> getParsedData() {
		return list;
	}

	/* XML文件開始解析時呼叫此method */
	@Override
	public void startDocument() throws SAXException {
		if (list == null) {
			list = new ArrayList<QueryPersonCouponBean>();
		}
	}

	/* XML文件結束解析時呼叫此method */
	@Override
	public void endDocument() throws SAXException {
	}

	/* 解析到Element的開頭時呼叫此method */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {

			if (localName.equals("item")) {
				this.in_item = true;
				/* 解析到item的開頭時new一個News物件 */
				queryPersonCouponBean = new QueryPersonCouponBean();
				Log.d("ken", buf.toString());
			} else if (localName.equals("gift_id")) {
				if (this.in_item) {
					this.in_gift_id = true;
				}
			} else if (localName.equals("gift_code")) {
				if (this.in_item) {
					this.in_gift_code = true;
				}
			} else if (localName.equals("gift_name")) {
				if (this.in_item) {
					this.in_gift_name = true;
				}
			} else if (localName.equals("date")) {
				if (this.in_item) {
					this.in_date = true;
				}
			} else if (localName.equals("date_display")) {
				if (this.in_item) {
					this.in_date_display = true;
				}
			} 

		buf.setLength(0);
	}

	/* 解析到Element的結尾時呼叫此method */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("item")) {
			this.in_item = false;
			/* 解析到item的結尾時將News物件寫入List中 */
			list.add(queryPersonCouponBean);
		} else if (localName.equals("gift_id")) {
			if (this.in_item) {
				/* 設定News物件的title */
				queryPersonCouponBean.setGiftId(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_gift_id = false;
			} 
		} else if (localName.equals("gift_code")) {
			if (this.in_item) {
				/* 設定News物件的link */
				queryPersonCouponBean.setGiftCode(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_gift_code = false;
			}
		} else if (localName.equals("gift_name")) {
			if (in_item) {
				queryPersonCouponBean.setGiftName(buf.toString().trim());
				buf.setLength(0);
				this.in_gift_name = false;
			}
		} else if (localName.equals("date")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				queryPersonCouponBean.setDate(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_date = false;
			}
		} else if (localName.equals("date_display")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				queryPersonCouponBean.setDateDisplay(buf.toString().trim());
				buf.setLength(0);
				this.in_date_display = false;
			}
		}
	}

	/* 取得Element的開頭結尾中間夾的字串 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_item || this.in_date_display) {
			/* 將char[]加入StringBuffer */
			buf.append(ch, start, length);
		}
	}
}